//
//  ALWelcomeController.swift
//  ALSinaWeiBoSwift
//
//  Created by 柴瑞军 on 16/4/12.
//  Copyright © 2016年 myCompany. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class ALWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
//      添加控件
        preparaUI()
        
        
//        网络加载请求数据
        ALUserAccountViewModel.userAccountViewModel.loadUserInfo { (error) in
            if error != nil{
            
                ALPrint("网络加载错误")
            }
            ALPrint("网络加载成功")
            
//            获取用户信息---网络下载图片
            self.setIconImage()
            
        }
    }
    private func setIconImage(){
        if let avatar_large =  ALUserAccountViewModel.userAccountViewModel.userAccount?.avatar_large {
            // 网络请求完了在调用这段代码
            // 使用SD来下载图片
            let url = NSURL(string: avatar_large)
           iconView.sd_setImageWithURL(url, placeholderImage:  UIImage(named: "avatar_default_big"))
        }
    }
//    在view看得见的时候开始执行头像的动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconVAnimation()
    }
    
//    开始执行头像的动画
    private func iconVAnimation(){
//    改变头像底部的约束
        iconView.snp_updateConstraints { (make) in
           make.bottom.equalTo(view.snp_bottom).offset(-(UIScreen.mainScreen().bounds.height-160))
        }
//        print("\(iconView.frame)")
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: UIViewAnimationOptions(rawValue:0), animations: {
//            通知系统来重新进行布局
            self.view.layoutIfNeeded()
//            print("\(self.iconView.frame)")

            }) { (_) in
//                让label显示出来
                UIView.animateWithDuration(alDefaultTimeInterval, animations: { 
                    self.welcomeLabel.hidden = false

                    }, completion: { (_) in
//                        动画完成了
                        ALPrint("动画完成了,要切换界面")
                        
                        let controller = ALMainTabBarController()
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
                })
        }
    }
    
    
    private func preparaUI(){
//        1.添加控件
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
//        2.添加约束
        bgImageView.translatesAutoresizingMaskIntoConstraints = true
        iconView.translatesAutoresizingMaskIntoConstraints = true
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = true
        addContraint()
    }
    
//    设置约束
    private func addContraint(){
//        背景图片的约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(view)
        }
//       头像的约束
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
        }
//        欢迎label的约束
        welcomeLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
//    懒加载控件
//    1.背景
    private lazy var bgImageView:UIImageView = UIImageView.init(image: UIImage.init(named: "ad_background"))
//    2.头像
    private lazy var iconView:UIImageView = {
    let iconV = UIImageView.init(image: UIImage.init(named: "avatar_default_big"))
        
//        设置圆角
        iconV.layer.cornerRadius = iconV.bounds.width*0.5
        iconV.layer.masksToBounds = true
        
        return iconV
    }()
//   3.label
    private let welcomeLabel:UILabel = {
    let welcomeL = UILabel()
        
        welcomeL.text = "欢迎登录"
        welcomeL.textColor = UIColor.blackColor()
        welcomeL.font = welcomeFont
//        特别注意下面的属性
        welcomeL.sizeToFit()
        
        return welcomeL
    }()
}
